package com.rongke.web.controller.medical;

import com.alibaba.fastjson.JSONObject;
import com.rongke.commons.JsonResp;
import com.rongke.model.OperationRecord;
import com.rongke.model.dict.DictOperation;
import com.rongke.model.dict.DictOperationType;
import com.rongke.service.DictOperationService;
import com.rongke.service.DictOperationTypeService;
import com.rongke.service.OperationRecordService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 手术记录
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/operationRecord")
@Transactional
public class OperationRecordController {
	private static final Logger LOGGER = Logger
			.getLogger(OperationRecordController.class);

	@Autowired
	private OperationRecordService operationRecordService;

	@Autowired
	private DictOperationService dictOperationService;

	@Autowired
	private DictOperationTypeService dictOperationTypeService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResp addOperationRecord(
			@RequestBody OperationRecord operationRecord) {

		try {
			LOGGER.debug("addOperationRecord");

			// convertNameToId(operationRecord);
			operationRecordService.insert(operationRecord);
			JSONObject json = new JSONObject();
			json.put("id", operationRecord.getId());

			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("addOperationRecord failed", e);
			return JsonResp.fa(null);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResp updateOperationRecord(
			@RequestBody OperationRecord operationRecord) {
		LOGGER.debug("updateOperationRecord");

		try {
			operationRecord.setGmtCreate(null);// 不修改创建时间
			operationRecord.setGmtModified(new Date());
			operationRecord.setPicContent(null);
			operationRecord.setUserId(null);
			operationRecord.setMedicalRecordId(null);
			// convertNameToId(operationRecord);

			operationRecordService.updateById(operationRecord);

			JSONObject json = new JSONObject();
			json.put("id", operationRecord.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("updateOperationRecord failed", e);
			return JsonResp.fa(null);

		}
	}

	/**
	 * 根据ID删除检查记录
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public JsonResp delOperationRecordById(long id) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("delOperationRecordById, id=" + id);
		}

		try {
			operationRecordService.deleteById(id);

			JSONObject json = new JSONObject();
			json.put("id", id);
			return JsonResp.ok(json);
		} catch (Exception e) {
			LOGGER.error("delOperationRecordById failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @根据id查找病历记录报告
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectOperationRecord(Long id) {
		LOGGER.debug("selectOperationRecord");

		try {
			OperationRecord operationRecord = operationRecordService
					.selectById(id);
			// convertIdToName(operationRecord);
			return JsonResp.ok(operationRecord);
		} catch (Exception e) {
			LOGGER.error("selectOperationRecord failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据具体的病历，获取所有的医生诊断书
	 * 
	 * @param medicalRecordId
	 * @return
	 */
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResp selectList(long medicalRecordId) {
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("selectList, medicalRecordId=" + medicalRecordId);
		}

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("medical_record_id", medicalRecordId);
			List<OperationRecord> result = operationRecordService
					.selectByMap(params);
			// for (OperationRecord record : result) {
			// convertIdToName(record);
			// }
			return JsonResp.ok(result);
		} catch (Exception e) {
			LOGGER.error("selectList failed, medicalRecordId="
					+ medicalRecordId, e);
			return JsonResp.fa(null);
		}
	}

	// private void convertIdToName(OperationRecord operation) {
	// if (null == operation) {
	// return;
	// }
	// // 转换手术名称
	// List<DictOperation> dictOperationList = dictOperationService
	// .selectByMap(null);
	// for (DictOperation dictOperation : dictOperationList) {
	// if (operation.getName().equals(dictOperation.getId())) {
	// operation.setName(dictOperation.getName());
	// }
	// }
	//
	// List<DictOperationType> dictList = dictOperationTypeService
	// .selectByMap(null);
	// for (DictOperationType dictOperationType : dictList) {
	// if (operation.getType().equals(dictOperationType.getId())) {
	// operation.setType(dictOperationType.getName());
	// }
	// }
	// }
	//
	// private void convertNameToId(OperationRecord operation) {
	// if (null == operation) {
	// return;
	// }
	//
	// // 转换手术名称
	// List<DictOperation> dictOperationList = dictOperationService
	// .selectByMap(null);
	// for (DictOperation dictOperation : dictOperationList) {
	// if (operation.getName().equals(dictOperation.getName())) {
	// operation.setName(String.valueOf(dictOperation.getId()));
	// }
	// }
	//
	// List<DictOperationType> dictList = dictOperationTypeService
	// .selectByMap(null);
	// for (DictOperationType dictOperationType : dictList) {
	// if (operation.getType().equals(dictOperationType.getName())) {
	// operation.setType(String.valueOf(dictOperationType.getId()));
	// }
	// }
	// }
}
